package com.epalmpay.controller.channel.activity;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.activity.CouponAddInDTO;
import com.epalmpay.dto.activity.CouponDetailOutDTO;
import com.epalmpay.dto.activity.CouponInDTO;
import com.epalmpay.dto.activity.CouponListOutDTO;
import com.epalmpay.dto.activity.CouponOutDTO;
import com.epalmpay.entity.Coupon;
import com.epalmpay.entity.Page;
import com.epalmpay.service.activity.IActivityCouponService;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Tools;

@Controller
@RequestMapping(value="/channel/activity")
public class CouponController extends BaseController {
     
	@Resource
	private IActivityCouponService activityCouponService;
	@Resource
	private ISysLogService sysLogService;
	
	@RequestMapping(value="/couponIndex")
	@ResponseBody
	public CouponListOutDTO  couponIndex(CouponInDTO couponInDTO){
		CouponListOutDTO couponListOutDTO = new CouponListOutDTO();
		try {
			couponListOutDTO = activityCouponService.getToIndexData(couponInDTO);
			couponListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return couponListOutDTO;
		} catch (ApplicationException ae) {
			couponListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			couponListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return couponListOutDTO;
	}

	/**
	 * 显示优惠券列表(放弃)
	 * 
	 */
	//@RequestMapping(value="/couponList")
	@ResponseBody
	public CouponListOutDTO  couponListOld(CouponInDTO couponInDTO,PageDTO pageDTO){
		CouponListOutDTO couponListOutDTO=new CouponListOutDTO();

		try {
			Page page=Tools.pageEmtity(pageDTO);
			
            //查询时取数据			
			if(couponInDTO.getIsReload()){
				//获取红包列表信息
				List<CouponOutDTO> couponOutDTOList = activityCouponService.
						getCouponList(couponInDTO, page);
				couponListOutDTO.setCouponOutDTOList(couponOutDTOList);
			}
			couponListOutDTO.setPage(page); 
			couponListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return couponListOutDTO;
		} catch (ApplicationException ae) { 
			couponListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		 catch (Exception e) { 
			 couponListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			 logger.error(e);
		}
		return couponListOutDTO;
	}

	/**
	 * 显示优惠券列表
	 *
	 */
	@RequestMapping(value="/couponList")
	@ResponseBody
	public CouponListOutDTO  couponList(@RequestBody CouponInDTO couponInDTO){
		CouponListOutDTO couponListOutDTO=new CouponListOutDTO();

		try {
			//Page page=Tools.pageEmtity(pageDTO);

			//查询时取数据
			if(couponInDTO.getIsReload()){
				//获取红包列表信息
				couponListOutDTO = activityCouponService.getCouponList(couponInDTO);
				//couponListOutDTO.setCouponOutDTOList(couponOutDTOList);
			}
			//couponListOutDTO.setPage(page);
			couponListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return couponListOutDTO;
		} catch (ApplicationException ae) {
			couponListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			couponListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return couponListOutDTO;
	}

	@RequestMapping(value="/getSepcialActivityCouponList")
	@ResponseBody
	public CouponListOutDTO  GetSepcialActivityCouponList(CouponInDTO couponInDTO){
		CouponListOutDTO couponListOutDTO=new CouponListOutDTO();
		try {

			//查询时取数据
//			if(couponInDTO.getIsReload()){
				//获取红包列表信息
				List<CouponOutDTO> couponOutDTOList = activityCouponService.
						GetSpecialActivityCouponList(couponInDTO);
				couponListOutDTO.setCouponOutDTOList(couponOutDTOList);
//			}

			couponListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return couponListOutDTO;
		} catch (ApplicationException ae) {
			couponListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			couponListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return couponListOutDTO;
	}

	/**
	 * 保存优惠卷
	 * @param couponAddInDTO
	 * @return
	 */
	@RequestMapping(value="/couponSave")
	@ResponseBody
	public CouponListOutDTO  CouponSave(@RequestBody CouponAddInDTO couponAddInDTO){
		CouponListOutDTO couponListOutDTO=new CouponListOutDTO();
		try {

		    activityCouponService.saveCoupon(couponAddInDTO,HOME_URL);
			couponListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			
			String content ="新增优惠券";
			sysLogService.addSysLog(couponAddInDTO.getUserId(),couponAddInDTO.getUserName(),null,content,couponAddInDTO.getGroupId());
			
			return couponListOutDTO;
		} catch (ApplicationException ae) {
			couponListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			couponListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return couponListOutDTO;
	}

	@RequestMapping(value="/couponDetail")
	@ResponseBody
	public CouponDetailOutDTO  couponDetail(CouponInDTO couponInDTO,PageDTO pageDTO){
		CouponDetailOutDTO result = new CouponDetailOutDTO();
		try {
			Page page=Tools.pageEmtity(pageDTO);
			result = activityCouponService.getCouponDetail(couponInDTO,page);
			result.setPage(page); 
			result.setErrorCode(Rescode.SUCCESS, "处理成功");
			return result;
		} catch (ApplicationException ae) {
			result.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
			logger.error(ae);
		}
		catch (Exception e) {
			result.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		return result;
	}

	
	/**
	 * 通过会员id查询coupon 编辑时回显
	 * @param couponInDTO
	 * @return
	 */
	@RequestMapping(value="/goEditCoupon")
	@ResponseBody
	public CouponOutDTO  queryCouponById(CouponInDTO couponInDTO){
		CouponOutDTO couponOutDTO = new CouponOutDTO();
		Coupon coupon = new Coupon();
		try {   
			coupon.setId(couponInDTO.getId());
			Coupon coupons=activityCouponService.queryCouponById(coupon);

			if(coupons!=null){
				PropertyCopyUtil.copyPropertiesExclude(coupons,couponOutDTO);
				couponOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功");
				return couponOutDTO;
			}else{
				couponOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (Exception e) { 
			couponOutDTO.setErrorCode("-1", "处理出现异常");          
			e.printStackTrace();
			logger.error(e);
		}
		couponOutDTO.setErrorCode("-2", "处理失败");
		return couponOutDTO;
	}
	
	/**
	 * 修改优惠卷信息
	 * @param
	 * @return
	 */
	@RequestMapping(value="/updateCoupon")
	@ResponseBody
	public CouponOutDTO  updateCoupon(@RequestBody CouponInDTO couponInDTO){
		CouponOutDTO couponOutDTO=new CouponOutDTO();

		try {   
			activityCouponService.updateCoupon(couponInDTO);
			couponOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
			
			String content ="修改优惠券信息";
			sysLogService.addSysLog(couponInDTO.getUserId(),couponInDTO.getUserName(),null,content,couponInDTO.getGroupId());
			
			return couponOutDTO;
		} catch (Exception e) { 
			couponOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		couponOutDTO.setErrorCode("-2", "处理失败");
		return null;
	}
	
	/**
	 * 结束优惠卷
	 * @param
	 * @return
	 */
	@RequestMapping(value="/updateCouponStatus")
	@ResponseBody
	public CouponOutDTO  updateCouponStatus(CouponInDTO couponInDTO){
		CouponOutDTO couponOutDTO=new CouponOutDTO();
		try {
			activityCouponService.endCouponStatus(couponInDTO.getId());
			couponOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");

			String content ="修改优惠券的状态";
			sysLogService.addSysLog(couponInDTO.getUserId(),couponInDTO.getUserName(),null,content,couponInDTO.getGroupId());
			
			return couponOutDTO;
		} catch (Exception e) { 
			couponOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		couponOutDTO.setErrorCode("-2", "处理失败");
		return null;
	}
	
	/**
	 * 修改可选门店
	 * @param couponAddInDTO
	 * @return
	 */
	@RequestMapping(value="/updateShop")
	@ResponseBody
	public CouponOutDTO updateShop(CouponAddInDTO couponAddInDTO){
		CouponOutDTO couponOutDTO=new CouponOutDTO();
		Coupon coupon = new Coupon();
		try {   

			coupon.setId(couponAddInDTO.getId());
			coupon.setShopids(couponAddInDTO.getShopids());
			activityCouponService.updateShop(coupon);
			couponOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
			
			String content ="修改优惠券的状态";
			sysLogService.addSysLog(couponAddInDTO.getUserId(),couponAddInDTO.getUserName(),null,content,couponAddInDTO.getGroupId());
			
			return couponOutDTO;
		} catch (Exception e) { 
			couponOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
			logger.error(e);
		}
		couponOutDTO.setErrorCode("-2", "处理失败");
		return null;
		
		
	}

	@RequestMapping("/getCouponListById")
	@ResponseBody
	public List<Map<String, Object>> getCouponListById(@RequestBody Set<Long> couponIdSet) {
		return activityCouponService.getCouponListById(couponIdSet);
	}



	@RequestMapping("/getShareGoodListById")
	@ResponseBody
	public CouponListOutDTO getShareGoodListById(CouponAddInDTO couponAddInDTO) {
		CouponListOutDTO couponListOutDTOt=  activityCouponService.getShareGoodListById(couponAddInDTO);
		return  couponListOutDTOt;
	}

	@RequestMapping("/getActivityListById")
	@ResponseBody
	public CouponListOutDTO getActivityListById(CouponAddInDTO couponAddInDTO) {
		CouponListOutDTO couponListOutDTOt=  activityCouponService.getActivityListById(couponAddInDTO);
		return  couponListOutDTOt;
	}


}
